import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 加减法口算练习系统-v0.3
 * 支持生成无重复算式，并确保包含加法和减法
 */
public class CalculationSystemV03 {
    public static void main(String[] args) {
        // 输出系统标题
        System.out.println("===== 加减法口算练习系统-v0.3 =====");
        
        // 创建随机数生成器
        Random random = new Random();
        
        // 使用Set存储已生成的算式，确保无重复
        Set<String> problemSet = new HashSet<>();
        
        // 定义每行显示的题目数量
        final int PROBLEMS_PER_ROW = 2;
        
        // 需要生成的题目总数
        final int TOTAL_PROBLEMS = 50;
        
        // 确保至少包含一定数量的加法和减法题目
        final int MIN_ADDITIONS = 15;
        final int MIN_SUBTRACTIONS = 15;
        
        // 用于存储题目的数组
        String[][] problems = new String[(int)Math.ceil(TOTAL_PROBLEMS / (double)PROBLEMS_PER_ROW)][PROBLEMS_PER_ROW];
        String[][] answers = new String[(int)Math.ceil(TOTAL_PROBLEMS / (double)PROBLEMS_PER_ROW)][PROBLEMS_PER_ROW];
        
        // 已生成的加法和减法题目数量
        int additionCount = 0;
        int subtractionCount = 0;
        
        // 生成题目
        while (problemSet.size() < TOTAL_PROBLEMS) {
            // 确定运算类型：如果加法或减法数量不足，优先生成对应类型
            boolean isAddition;
            if (additionCount < MIN_ADDITIONS) {
                isAddition = true;
            } else if (subtractionCount < MIN_SUBTRACTIONS) {
                isAddition = false;
            } else {
                // 否则随机选择
                isAddition = random.nextBoolean();
            }
            
            int num1, num2, result;
            String problemKey;
            String problemString;
            String answerString;
            
            if (isAddition) {
                // 加法：确保和不超过100
                num1 = random.nextInt(100) + 1; // 1到100
                num2 = random.nextInt(101 - num1); // 0到(100-num1)
                result = num1 + num2;
                
                // 生成算式的唯一标识（考虑加法交换律，a+b和b+a视为同一题）
                if (num1 > num2) {
                    problemKey = "ADD:" + num2 + "+" + num1;
                } else {
                    problemKey = "ADD:" + num1 + "+" + num2;
                }
                
                problemString = num1 + " + " + num2 + " = ";
                answerString = "" + result;
            } else {
                // 减法：确保差不小于0
                num2 = random.nextInt(100); // 0到99
                num1 = num2 + random.nextInt(101 - num2); // num2到100
                result = num1 - num2;
                
                // 生成算式的唯一标识
                problemKey = "SUB:" + num1 + "-" + num2;
                
                problemString = num1 + " - " + num2 + " = ";
                answerString = "" + result;
            }
            
            // 检查是否已存在
            if (!problemSet.contains(problemKey)) {
                // 添加到集合中
                problemSet.add(problemKey);
                
                // 更新计数
                if (isAddition) {
                    additionCount++;
                } else {
                    subtractionCount++;
                }
                
                // 计算存储位置
                int index = problemSet.size() - 1;
                int row = index / PROBLEMS_PER_ROW;
                int col = index % PROBLEMS_PER_ROW;
                
                // 存储题目和答案
                problems[row][col] = "第" + (index + 1) + "题: " + problemString;
                answers[row][col] = "答案: " + answerString;
            }
        }
        
        // 输出题目，每行显示PROBLEMS_PER_ROW个题目
        System.out.println("\n===== 题目部分 =====");
        int totalRows = (int)Math.ceil(TOTAL_PROBLEMS / (double)PROBLEMS_PER_ROW);
        for (int i = 0; i < totalRows; i++) {
            StringBuilder problemRow = new StringBuilder();
            
            for (int j = 0; j < PROBLEMS_PER_ROW; j++) {
                int index = i * PROBLEMS_PER_ROW + j;
                if (index < TOTAL_PROBLEMS && problems[i][j] != null) {
                    // 格式化每个题目，确保对齐
                    problemRow.append(String.format("%-30s", problems[i][j]));
                }
            }
            
            System.out.println(problemRow.toString());
        }
        
        // 输出答案，每行显示PROBLEMS_PER_ROW个答案
        System.out.println("\n===== 答案部分 =====");
        for (int i = 0; i < totalRows; i++) {
            StringBuilder answerRow = new StringBuilder();
            
            for (int j = 0; j < PROBLEMS_PER_ROW; j++) {
                int index = i * PROBLEMS_PER_ROW + j;
                if (index < TOTAL_PROBLEMS && answers[i][j] != null) {
                    // 格式化每个答案，确保对齐
                    answerRow.append(String.format("%-30s", answers[i][j]));
                }
            }
            
            System.out.println(answerRow.toString());
        }
        
        System.out.println("\n===== 练习结束 =====");
        System.out.println("\n统计信息：");
        System.out.println("- 加法题目数量: " + additionCount);
        System.out.println("- 减法题目数量: " + subtractionCount);
    }
}